﻿/*
 *	Filename: wuyaowei_reverse.c
 *	 Version: 0.1.0
 *   Description: 单链表反转
 *
 *        Author: wuyaowei
 *          Date: 2020-06-10
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node{
  char data;
  struct Node * next;
}Node;

/*
 *  创建节点
 */
Node * createNode(char val) {
  Node *node = (Node *)malloc(sizeof(Node));
  node->data = val;
  node->next = NULL;
  return node;
}

/*
 * 链表反转
 *
 * 返回值：返回首节点指针
 */

Node * reverseList(Node * head) {
  Node * pBack = NULL;
  Node * p;

  while(NULL != head) {
    p = head;
    head = head->next;
    p->next = pBack;
    pBack = p;
  }
  return p;
}


void main() {
  char str[100];
  Node *head = NULL;
  printf("输入链表数据：\n");
  scanf("%s",str);

  int len = strlen(str);

  //初始化单链表
  Node* p;
  for (int i = 0; i <= len; i++) {
       Node *t1 = createNode(str[i]);
       if (i == 0) {
         head = t1;
         p = t1;
         continue;	 
       }
       p->next = t1;
       p = t1;
  }

  head = reverseList(head);
  printf("反转后链表遍历输出:\n");
  while(NULL != head) {
     printf("%c ", head->data);
     head = head->next;
  }
  printf("\n");

}
